package com.jysz.certificate.persistence.model.mf;

import java.util.Date;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.jysz.certificate.persistence.enums.mf.BillStatus;
import com.jysz.certificate.persistence.model.system.Financial;
import com.jysz.certificate.persistence.model.system.Shop;
import com.jysz.webcore.ssh.entity.AbstractBaseEntity;

/**
 * 发票
 * @author xuxiaolong
 */
@Entity
@Table(name = "mf_bill_")
@org.hibernate.annotations.Entity
public class Bill extends AbstractBaseEntity<Long> {

	private static final long serialVersionUID = -7906256280568123355L;
	
	/** 发票申请单*/
	private BillApply  billApply;
	
	/** 发票号*/
	private String billNum;
	
	/** 发票名*/
	private String billName;
	
	/** 付款方名称*/
	private String paymentName;
	
	/** 店铺*/
	private Shop shop;
	
	/** 金融机构*/
	private Financial financial;
	
	/** 应收金额(总和)*/
	private Double receivableAmount;
	
	/** 实收金额(总和)*/
	private Double factAmount;
	
	/** 开票日期*/
	private Date openDate;
	
	/** 申请日期*/
	private Date applyDate;
	
	/** 完全实收日期*/
	private Date incomeDate;
	
	/** 是否单独开票（金融机构）*/
	private Boolean aloneOpen;
	/** 是否单独开票(月份)*/
	private Boolean aloneMonth;
	
	/** 发票状态*/
	private BillStatus status;
	
	/** 费用明细*/
	private List<BillOption> options;
	
	/** 开票项目*/
	private String billRemark;
	
	
	@Column(name = "_bill_remark", nullable = true,columnDefinition="varchar(255)")
	public String getBillRemark() {
		return billRemark;
	}
	public void setBillRemark(String billRemark) {
		this.billRemark = billRemark;
	}
	public Bill(){}
	public Bill(Bill b,String billNum,String billName,Double receivableAmount,BillStatus status){
		this.billApply=b.getBillApply();
		this.billNum=billNum;
		this.billName=billName;
		this.receivableAmount=receivableAmount;
		this.paymentName=b.getPaymentName();
		this.shop=b.getShop();
		this.financial=b.getFinancial();
		this.openDate=b.getOpenDate();
		this.applyDate=b.getApplyDate();
		this.aloneOpen=b.getAloneOpen();
		this.aloneMonth=b.getAloneMonth();
		this.status=status;
	}
	
	public Bill(Bill b, List<BillOption> opts){
		this.billApply = b.getBillApply();
		this.billNum = b.getBillNum();
		this.billName=b.getBillName();
		this.paymentName=b.getPaymentName();
		this.shop=b.getShop();
		this.financial=b.getFinancial();
		this.openDate=b.getOpenDate();
		this.applyDate=b.getApplyDate();
		this.aloneOpen=b.getAloneOpen();
		this.aloneMonth=b.getAloneMonth();
		this.status=b.getStatus();
		Double sum=0D;
//		List<Bill> bills;
		for(BillOption bo:opts){
			/*bills=new ArrayList<Bill>();
			bills.add(this);
			bo.setBills(bills);*/
//			bo.setId(null);
			sum+=bo.getAmount();
		}
		this.receivableAmount = sum;
		this.options = opts;
	}
	
	
	@ManyToOne(targetEntity=Financial.class,cascade=CascadeType.REFRESH)
	@JoinColumn(name="_financial_id", insertable=true, updatable=true,columnDefinition="bigint(20)")
	public Financial getFinancial() {
		return financial;
	}

	public void setFinancial(Financial financial) {
		this.financial = financial;
	}

	@ManyToOne(cascade = CascadeType.REFRESH,fetch = FetchType.LAZY)  
	@JoinColumn(name = "_bill_apply_id", insertable=true, updatable=true,columnDefinition="bigint(20)")
	public BillApply getBillApply() {
		return billApply;
	}

	public void setBillApply(BillApply billApply) {
		this.billApply = billApply;
	}

	@Column(name = "_bill_num", nullable = true,columnDefinition="varchar(255)")
	public String getBillNum() {
		return billNum;
	}

	public void setBillNum(String billNum) {
		this.billNum = billNum;
	}

	@Column(name = "_bill_name", nullable = true,columnDefinition="varchar(255)")
	public String getBillName() {
		return billName;
	}

	public void setBillName(String billName) {
		this.billName = billName;
	}

	@ManyToOne(targetEntity=Shop.class,cascade=CascadeType.REFRESH)
	@JoinColumn(name="_shop_id", insertable=true, updatable=true,columnDefinition="bigint(20)")
	public Shop getShop() {
		return shop;
	}

	public void setShop(Shop shop) {
		this.shop = shop;
	}

	@Column(name = "_receivable_amount", nullable = true,columnDefinition="numeric(8,2)")
	public Double getReceivableAmount() {
		return receivableAmount;
	}
	public void setReceivableAmount(Double receivableAmount) {
		this.receivableAmount = receivableAmount;
	}
	@Column(name = "_payment_name", nullable = true,columnDefinition="varchar(255)")
	public String getPaymentName() {
		return paymentName;
	}

	public void setPaymentName(String paymentName) {
		this.paymentName = paymentName;
	}
	@Column(name = "_fact_amount", nullable = true,columnDefinition="numeric(8,2)")
	public Double getFactAmount() {
		return factAmount;
	}

	public void setFactAmount(Double factAmount) {
		this.factAmount = factAmount;
	}

	@Column(name = "_open_date", nullable = true)
	public Date getOpenDate() {
		return openDate;
	}

	public void setOpenDate(Date openDate) {
		this.openDate = openDate;
	}
	@Column(name = "_apply_date", nullable = true)
	public Date getApplyDate() {
		return applyDate;
	}

	public void setApplyDate(Date applyDate) {
		this.applyDate = applyDate;
	}

	@Column(name = "_status", nullable = false,columnDefinition="int(11)")
	public BillStatus getStatus() {
		return status;
	}

	public void setStatus(BillStatus status) {
		this.status = status;
	}

	//@ManyToMany(targetEntity = BillOption.class, cascade = { CascadeType.MERGE }, mappedBy = "bills")
	//@Fetch(FetchMode.SUBSELECT)
	
	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "rs_mf_bill_option_", joinColumns = @JoinColumn(name = "_bill_id"), inverseJoinColumns = @JoinColumn(name = "_option_id"))
	public List<BillOption> getOptions() {
		return options;
	}
	
	public void setOptions(List<BillOption> options) {
		this.options = options;
	}

	@Column(name = "aloneOpen", nullable = true,columnDefinition="bit(1)")
	public Boolean getAloneOpen() {
		return aloneOpen;
	}

	public void setAloneOpen(Boolean aloneOpen) {
		this.aloneOpen = aloneOpen;
	}
	
	@Column(name = "aloneMonth", nullable = true,columnDefinition="bit(1)")
	public Boolean getAloneMonth() {
		return aloneMonth;
	}
	public void setAloneMonth(Boolean aloneMonth) {
		this.aloneMonth = aloneMonth;
	}
	
	@Column(name = "_income_date", nullable = true)
	public Date getIncomeDate() {
		return incomeDate;
	}
	public void setIncomeDate(Date incomeDate) {
		this.incomeDate = incomeDate;
	}
	
	public boolean equals(Object obj){
		Bill b=(Bill) obj;
		if(this.getId()==b.getId())
			return true;
		return false;
	}

}
